package cn.vcjf.weapp.api.active.controller;


import cn.vcjf.weapp.api.active.entity.GoodsActiveRecord;
import cn.vcjf.weapp.api.active.entity.UserActive;
import cn.vcjf.weapp.api.active.service.GoodsActiveRecordService;
import cn.vcjf.weapp.api.active.service.UserActiveService;
import cn.vcjf.weapp.api.goods.entity.GoodsOrder;
import cn.vcjf.weapp.common.web.TableResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-26
 */
@Controller
@RequestMapping("/active/goodsCollageOrder")
public class GoodsActiveRecordController {
	@Resource
	private GoodsActiveRecordService goodsActiveRecordService;
	@Resource
	private UserActiveService userActiveService;

	/**
	 * 发起拼团
	 *
	 * @param goodsActiveRecord
	 * @return
	 */
	@RequestMapping("/addGoodsCollageOrder")
	@ResponseBody
	@Transactional
	public TableResult addGoodsCollageOrder(GoodsActiveRecord goodsActiveRecord,@Valid GoodsOrder goodsOrder, BindingResult result) {

		TableResult.checkParams(result);
		return goodsActiveRecordService.addGoodsCollageOrder(goodsActiveRecord,goodsOrder);
	}

	/**
	 * 拼团同步锁
	 */
	public static Map<Integer, GoodsActiveRecord> joinCollageMap = new ConcurrentHashMap<>();
	/**
	 * 参与拼团
	 * @param goodsActiveRecord
	 * @return
	 */
	@RequestMapping("/joinGoodsCollage")
	@ResponseBody
	public TableResult joinGoodsCollage(GoodsActiveRecord goodsActiveRecord, GoodsOrder goodsOrder) {
		TableResult tableResult = null;
		Integer id = goodsActiveRecord.getId();
		if(joinCollageMap.containsKey(id)){
			GoodsActiveRecord collageOrder = joinCollageMap.get(id);
			synchronized (collageOrder){
				tableResult = goodsActiveRecordService.doJoinGoodsCoollage(goodsActiveRecord,goodsOrder);
			}
		}else {
			joinCollageMap.put(id, goodsActiveRecord);
			joinGoodsCollage(goodsActiveRecord,goodsOrder);
		}

		return tableResult;
	}

	/**
	 * 取消拼团
	 * @param goodsActiveRecord
	 * @return
	 */
	@RequestMapping("/cancelGoodsCollage")
	@ResponseBody
	public TableResult cancelGoodsCollage(GoodsActiveRecord goodsActiveRecord) {
		TableResult tableResult = goodsActiveRecordService.cancelGoodsCollage(goodsActiveRecord);

		return tableResult;
	}
	/**
	 * 参与砍价
	 * @param goodsActiveRecord
	 * @return
	 */
	@RequestMapping("/joinBargaining")
	@ResponseBody
	public TableResult joinBargaining(GoodsActiveRecord goodsActiveRecord) {
		TableResult tableResult = goodsActiveRecordService.joinBargaining(goodsActiveRecord);

		return tableResult;
	}

	@RequestMapping("/updateGoodsCollageOrder")
	@ResponseBody
	public TableResult updateGoodsCollageOrder(GoodsActiveRecord goodsActiveRecord) {
		goodsActiveRecord.preUpdate();
		boolean b = goodsActiveRecordService.updateById(goodsActiveRecord);
		return TableResult.Result(b);
	}

	/**
	 * 获取活动和参与人详情
	 *
	 * @param goodsActiveRecord
	 * @return
	 */
	@RequestMapping("/findGoodsCollageOrderOne")
	@ResponseBody
	public TableResult findGoodsCollageOrderOne(GoodsActiveRecord goodsActiveRecord) {

		EntityWrapper<GoodsActiveRecord> wrapper = goodsActiveRecord.getQueryWrapper();
		//Todo

		GoodsActiveRecord result = goodsActiveRecordService.selectOne(wrapper);

		UserActive query = new UserActive();
		query.setSysUserId(goodsActiveRecord.getSysUserId());
		query.setGoodsActiveId(result.getGoodsActiveId());
		query.setCollageOrderId(goodsActiveRecord.getId());

		List<UserActive> userActives = userActiveService.selectActiveUserList(query);
		result.setActiveUserList(userActives);
		return TableResult.OKResult(result);
	}

	/**
	 * 获取进行中的活动列表
	 * @param goodsActiveRecord
	 * @return
	 */
	@RequestMapping("/findActiveList")
	@ResponseBody
	public TableResult findActiveList(GoodsActiveRecord goodsActiveRecord) {

		goodsActiveRecordService.updateExpireActiveRecord();

		Page<GoodsActiveRecord> page = goodsActiveRecord.getPageInfo();

		List<GoodsActiveRecord> list = goodsActiveRecordService.selectActivePage(page, goodsActiveRecord);

		return TableResult.OKResult(page.setRecords(list));
	}

	/**
	 * 发送小程序模板消息的发货通知
	 *
	 * @param goodsOrder
	 * @return
	 */
	@RequestMapping("/sendMinAppDeliveryMessage")
	@ResponseBody
	public TableResult sendMinAppDeliveryMessage(GoodsOrder goodsOrder) {
		JSONObject jsonObject = goodsActiveRecordService.sendMinAppDeliveryMessage(goodsOrder, null);
		if("10000".equals(jsonObject.get("code"))){
			TableResult.OKResult();
		}
		return TableResult.ErrorResult(jsonObject.getString("msg"));
	}
	@RequestMapping("/deleteGoodsCollageOrder")
	@ResponseBody
	public TableResult deleteGoodsCollageOrder(GoodsActiveRecord goodsActiveRecord) {
		goodsActiveRecord.preUpdate();
		boolean b = goodsActiveRecordService.deleteById(goodsActiveRecord.getId());
		return TableResult.Result(b);
	}

}
